<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Config extends CI_Controller {
	var $perpage = 20;

		public function __construct() {
		   parent::__construct();
		   if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin()) redirect('auth/login/'.str_replace("/", ".",$_SERVER['REQUEST_URI']));
		   $this->layout->setlayout('admin/layout');
		   $this->crumps->push('/admin/config/', 'Конфигурация');
		   $this->users_rights_model->check_rights(strtolower(get_class()), $this->router->get_method());
	   }

	public function index($id = 0)	{
		$data['pages'] = $pages = $this->db->select()->from('config')->order_by('config_id', 'asc')->get()->result_array();
		$form = new jQuickForm('simple');

		$form->setLabel("Добавление изображения");
		$gr = $form->insertGroup()->addClass('col col2');
		$gr->insertInputText('config_title')->setLabel('Название на русском опции')->addRuleRequired();
		$gr->insertInputText('config_name')->setLabel('Название опции')->addRuleRequired();
		$select = $gr->insertSelect('config_type')->setLabel('Тип опции');
		$select->addOption('Текстовое поле', '1');
		$select->addOption('Textarea', '2');

		if ($_SERVER['REQUEST_METHOD'] == "POST" && $form->validate()) {
			$p_data = $this->input->post();
			$this->db->insert('config', $p_data);
			$o = $this->db->insert_id();
			$this->session->set_flashdata('msg', 'Успешно добавлено');
			redirect('admin/config/edit/'.$o);
		}

		$form->insertInputSubmit("Добавить");

		$data['form'] = $form."";
		$data['js'] = jQuickForm::js()->getJsAll();

		$this->layout->view('admin/config/index', $data);
	}
	public function edit($id) {

		$this->crumps->push('/admin/config/'.($id == 0?'add':'edit/'.$id).'/', "Просмотр");
		if ($id != 0) $data['page'] = $page = $this->db->select()->from('config')->where('config_id', $id)->get()->row_array();
		if ($id != 0 && sizeof($page) <= 0) redirect('admin/config');
		$form = new jQuickForm('simple');
		//$form->setLabel('Редактирование опции');

		$form->insertInputText('config_title')->setLabel('Название')->addRuleRequired();

		if ($page['config_type'] == 1) {
			$form->insertInputText('config_value')->setLabel('Значение')->addRuleRequired();
		} else {
			$form->insertTextArea('config_value')->setLabel('Значение')->addRuleRequired();
		}
		if ($_SERVER['REQUEST_METHOD'] == "POST" && $form->validate()) {
			$p_data = $this->input->post();
			$this->db->where('config_id', $id)->update('config', $p_data);
			$this->session->set_flashdata('msg', 'Успешно обновлено');
			redirect('admin/config/edit/'.$id.'/');
		}

		$form->insertInputSubmit("Сохранить");

		$form->addDataSource(new HTML_QuickForm2_DataSource_Array($page));
		$data['form'] = $form."";
		$data['js'] = jQuickForm::js()->getJsAll();

		$this->layout->view('admin/config/show', $data);
	}
	public function add() {	}
	public function delete($id = 0) {
		if ($id != 0) $data['page'] = $page = $this->db->select()->from('config')->where('config_id', $id)->get()->result_array();
		if ($id != 0 && sizeof($page) <= 0) redirect('admin/config');
		$this->db->where('config_id', $id)->delete('config');
		rrmdir('./uploads/main_config/'.$id);
		$this->session->set_flashdata('msg', 'Успешно удалено');
		redirect('admin/config');
	}
}